/*
 * Copyright 2008 Nils Adermann
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Array.h"

using namespace JSON;

Array::Array() :
	Value(ARRAY)
{
	vector = new std::vector<Value*>();
}

Array::Array(const Array& a) :
	Value(a)
{
	vector = new std::vector<Value*>();
	*vector = *a.getVector();
}

Array::~Array()
{
	// the values will be deleted too
	for (std::vector<Value*>::iterator it = begin(); it != end(); ++it)
	{
		if (*it)
		{
			delete *it;
		}
	}
	delete vector;
}

bool Array::empty() const
{
	return vector->empty();
}

size_t Array::size() const
{
	return vector->size();
}

Value*& Array::at(size_t pos)
{
	return vector->at(pos);
}

const Value* Array::at(size_t pos) const
{
	return vector->at(pos);
}

Value*& Array::operator[](const size_t pos)
{
	return (*vector)[pos];
}

const Value* Array::operator[](const size_t pos) const
{
	return (*vector)[pos];
}

Array& Array::operator=(const Array& a)
{
	if (&a != this)
	{
		delete vector;
		vector = new std::vector<Value*>();
		*vector = *a.getVector();
	}
	return *this;
}

Value*& Array::front()
{
	return vector->front();
}

Value*& Array::back()
{
	return vector->back();
}

const Value* Array::front() const
{
	return vector->front();
}

const Value* Array::back() const
{
	return vector->back();
}

std::vector<Value*>::iterator Array::begin()
{
	return vector->begin();
}

std::vector<Value*>::iterator Array::end()
{
	return vector->end();
}

std::vector<Value*>::const_iterator Array::begin() const
{
	return vector->begin();
}

std::vector<Value*>::const_iterator Array::end() const
{
	return vector->end();
}

void Array::push_back(Value* value)
{
	vector->push_back(value);
}

void Array::pop_back()
{
	vector->pop_back();
}

void Array::insert(size_t index, Value* value)
{
	insert(begin() + index, value);
}

void Array::insert(std::vector<Value*>::iterator it, Value* value)
{
	vector->insert(it, value);
}

void Array::erase(size_t index)
{
	std::vector<Value*>::iterator position = begin() + index;
	if (*position)
	{
		delete *position;
		*position = NULL;
	}
	erase(begin() + index);
}

void Array::erase(std::vector<Value*>::iterator it)
{
	if (*it)
	{
		delete *it;
		*it = NULL;
	}
	vector->erase(it);
}

std::vector<Value*>* Array::getVector()
{
	return vector;
}

const std::vector<Value*>* Array::getVector() const
{
	return vector;
}

Array& Array::toArray()
{
	return *this;
}

const Array& Array::toArray() const
{
	return *this;
}

void Array::toJSON(UnicodeString* u) const
{
	u->append("[");

	for (std::vector<Value*>::const_iterator it = begin(); it != end(); ++it)
	{
		if (it != begin())
		{
			u->append(", ");
		}
		
		(*it)->toJSON(u);
	}

	u->append("]");
}
